package cn.ekuma.epos.datalogic.define.dao;

import cn.ekuma.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.db.table.I_AttributeSetInstance;
import com.openbravo.bean.AttributeSet;
import com.openbravo.bean.AttributeSetInstance;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SerializerReadString;
import com.openbravo.data.loader.SerializerWriteBasic;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

public class AttributeSetInstanceDAO  extends ModifiedLogDAO<AttributeSetInstance>{
	
	public AttributeSetInstanceDAO(I_Session s) {
		super(s);	
	}

	@Override
	public TableDefinition getTable() {
		return new TableDefinition(s,
                "ATTRIBUTESETINSTANCE",
                new Field[]{
                  new Field(I_AttributeSetInstance.ID, Datas.STRING, Formats.STRING),
                  new Field(I_AttributeSetInstance.ATTRIBUTESET_ID, Datas.STRING, Formats.STRING,AttributeSet.class),
                  new Field(I_AttributeSetInstance.DESCRIPTION, Datas.STRING, Formats.STRING),
                  new Field(I_AttributeSetInstance.DIFFPRICE, Datas.DOUBLE, Formats.DOUBLE),
                  new Field(I_AttributeSetInstance.LASTMODIFIED,Datas.TIMESTAMP, Formats.TIMESTAMP),
                  new Field(I_AttributeSetInstance.DIFFPRICECOST, Datas.DOUBLE, Formats.DOUBLE)
		},
        new int[]{0});
	}


	@Override
	public void writeInsertValues(DataWrite dp, AttributeSetInstance obj)
			throws BasicException {
		dp.setString(1,obj.getId());
		dp.setString(2,obj.getAttributeSetID());
		dp.setString(3,obj.getDescription());
		dp.setDouble(4,obj.getDiffPrice());
		dp.setTimestamp(5,obj.getLastModified());
		dp.setDouble(6,obj.getDiffPriceCost());
	}


	@Override
	public Class getSuportClass() {
		return AttributeSetInstance.class;
	}


	@Override
	public AttributeSetInstance readValues(DataRead dr, AttributeSetInstance obj)
			throws BasicException {
		if(obj==null)
			obj=new AttributeSetInstance();
		obj.setId(dr.getString(1));
		obj.setAttributeSetID(dr.getString(2));
		obj.setDescription(dr.getString(3));
		obj.setDiffPrice(dr.getDouble(4));
		obj.setLastModified(dr.getTimestamp(5));
		obj.setDiffPriceCost(dr.getDouble(6));
		return obj;
	}
	
	public String findAttributeSetInstanceId(String attsetid,String value) throws BasicException{
		return (String) new PreparedSentence(s,
                "SELECT ID FROM ATTRIBUTESETINSTANCE WHERE ATTRIBUTESET_ID = ? AND DESCRIPTION = ?",
                new SerializerWriteBasic(Datas.STRING, Datas.STRING),
                SerializerReadString.INSTANCE).find(new String[]{attsetid,value});
	}
}
